// 日期区间
laydate.render({
    elem: '#between'
    ,range: true
  });
// 车辆选择
$('.number').on('click',()=>{
   opsearch('{:url("html/car/car")}?op=search')
})


// 添加保险记录跳转
$('#add').on('click',()=>{
    window.location.href = "{:url('html/plus/insurance_add')}"
})

    // 查看删除的数据
    $('.delete_data').on('click',()=>{
        window.location.href= path.insurance.index+"?filter=delete";
    })
    //查看有用数据
    $('.use_data').on('click',()=>{
        window.location.href= path.insurance.index;
    })
var page = 1;
var pagenum = 10;
var fi=1;
var url={
    list:api.insurance.list,
    delete:api.insurance.delete,
}
var data={};
data.page=page;
data.pagenum=pagenum;
var where_json ={};

{if $Think.get.filter}
data.with_trashed=2
var main=[[ //表头
   {field: 'car', title: '车牌号', fixed: 'left',templet: '#carId',minWidth:110}
   ,{field: 'policy_number', title: '保险号'}
   ,{field: 'policy_holder', title: '投保人'}  
   ,{field: 'policy_type', title: '保险种类',templet:'<div>{{d.policy_type ==1?"交通强制险":"商业险"}}</div>',minWidth:110}
   ,{field:'service_id', title:'保险公司',templet:'<div>{{d.service.service_name?d.service.service_name:"无"}}</div>',minWidth:130}
   ,{field:'policy_charge', title:'保险费用'}
   ,{field:'start_date', title:'投保时间',minWidth:110}
   ,{field:'end_date', title:'到期时间',minWidth:110}
   ,{field:'ps', title:'备注说明'}
]];
{else/}
var main=[[ //表头
    {type:'checkbox', fixed: 'left'}
   ,{field: 'car', title: '车牌号', fixed: 'left',templet: '#carId',minWidth:110}
   ,{field: 'policy_number', title: '保险号'}
   ,{field: 'policy_holder', title: '投保人'}  
   ,{field: 'policy_type', title: '保险种类',templet:'<div>{{d.policy_type ==1?"交通强制险":"商业险"}}</div>',minWidth:110}
   ,{field:'service_id', title:'保险公司',templet:'<div>{{d.service.service_name?d.service.service_name:"无"}}</div>',minWidth:130}
   ,{field:'policy_charge', title:'保险费用'}
   ,{field:'start_date', title:'投保时间',minWidth:110}
   ,{field:'end_date', title:'到期时间',minWidth:110}
   ,{field:'ps', title:'备注说明'}
   ,{ title: '操作',minWidth:120, toolbar:'#demo',fixed: 'right',align:"center"}
]];
{/if}



{if $Think.get.policy_type}
where_json.policy_type=['in','{$Think.get.policy_type}']
{/if}

{if $Think.get.car_id}
where_json['car.car_id']=['in','{$Think.get.car_id}']
{/if}

{if $Think.get.service_name}
where_json['service.service_name']=['like','%{$Think.get.service_name}%']
{/if}

{if $Think.get.between}
var start = '{$Think.get.between}'.split(' ');
where_json.start_date=['between time',[start[0],start[2]]]
{/if}
// 重置
$('.reset').on('click',function(){
    $(this).closest('form').find('input').each(function(){
        $(this).val("")
    })
    $(this).closest('form').find('select').each(function(){
        $(this).val("")
    })
})
data.where_json=JSON.stringify(where_json)

ajaxsevice(data);
function ajaxsevice(data) {
    Load();
    //请求数据
    ajax(url.list, data, 'post').done(function (resp) {
        Load(1);
        // 成功回调
        //方法级表单渲染
        table.render({
            elem: '#insurance',
            cols: main,
            data: resp.data,
            limit: pagenum,
            cellMinWidth: 80
        });
        if(fi==1){
            laypage.render({
                elem: 'page'
                ,count: resp.count
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,theme: '#1E9FFF'
                ,jump: function(obj, first){
                    console.log(obj)
                    if(!first){
                        page=obj.curr;
                        data.page=page;
                        pagenum=obj.limit;
                        data.pagenum=pagenum;
                        ajaxsevice(data);
                    }else{
                        
                    }
                }
              })
              fi = 2;
        }
    
        table.on('tool(demo)', function(obj){
            var data1 = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var data2 = {
                ids:data1.id
            };
            if(layEvent === 'edit'){ 
                window.location.href = path.insurance.add+"?id="+data1.id;
            }else if(layEvent==='del'){
                layer.confirm('是否删除', function (index) {
                    Load()
                    ajax(url.delete, data2, 'post').done(function (res) {
                        // 成功回调
                        console.log(res)
                        layer.closeAll();
                        Msg('删除成功');
                        ajaxsevice(data)
                    }).fail(function (err) {
                        // 失败回调
                        layer.close(deleteload);
                        Err(res.msg ? res.msg : '操作失败，稍候再试');
                    });
                    layer.close(index);
                })
            }
        })
          			//删除多个
    			$(".delete-multiple").on('click', function () {
    				var ids = '';
                    var checkStatus = table.checkStatus('insurance'); //test即为基础参数id对应的值
                    console.log('多选信息')
                    console.log(checkStatus)
    				console.log(checkStatus.data) //获取选中行的数据
    				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                    console.log(checkStatus.isAll) //表格是否全选
                    if(checkStatus.data.length){
    				for (var dei = 0; dei < checkStatus.data.length; dei++) {
    					if (dei == 0) {
    						ids = checkStatus.data[dei].id
    					} else {
    						ids += ("," + checkStatus.data[dei].id)
    					}
                    }
                    console.log('删除')
                    console.log(ids)
                    var data2 ={
                        ids : ids
                    }
    				layer.open({
    					type: 1,
    					title: false,
    					id: "delete",
    					content: '<div style="padding: 20px 100px;">是否删除</div>',
    					btn: ['确定', '取消'],
    					btnAlign: 'c' //按钮居中
    						,
    					shade: 0 //不显示遮罩
    						,
    					yes: function () {
    						var deleteload = layer.load(1, {
    							shade: [0.1, '#fff'] //0.1透明度的白色背景
                            });
                            console.log(data2)
                            ajax(url.delete,data2,'post').done(function(resp) {
                                // 成功回调
                            			layer.close(deleteload);
                                        layer.closeAll();
                                        Msg('删除成功');
                                        ajaxsevice(data)
                                }).fail(function(err) {
                                // 失败回调
                                layer.close(deleteload);
    						    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
    					},
    					btn2: function () {
    						layer.closeAll();
    					}
                    });
                }else{
                    Err('未有选择项')
                }
                });
                if(resp.code ==1){

                }else{
                    Err(resp.msg)
                }
    }).fail(function (err) {
        // 失败回调
        Load(1)
    	Err('请求失败')
    });

};